*************************************************************************
**********************Table 3: Robustness Checks*************************
*************************************************************************

cd $PATH

** SURVEY WAVES: 1989, 1991, 1993, 1997, 2000, 2004, 2006, 2009, 2011, 2015**

use analysis_main.dta, clear

global score comm denc div econ health house market soc trans edc mart sani
global dependent loggini logtheil logpctratio_p75p25 logpctratio_p90p10

xtset commid WAVE

* 1. different time windows

* 1.1 restricting the sample: 1997-2000
eststo clear

preserve
keep if WAVE>=1997 & WAVE<=2000

foreach y in $dependent{
eststo: xtreg `y' dd post $score i.WAVE, fe vce(cluster commid)
}

esttab est1 est2 est3 est4 using robust1.tex, b(a2) se(a2) keep(dd) noobs  ///
		starlevels(* 0.10 ** 0.05 *** 0.01) mtitles("Gini" "Theil" "P90/P10" "P75/P25") ///
		nolabel nolines replace fragment prehead("\begin{tabular}{l*{4}{c}} \hline\hline")  ///
		posthead("\hline \multicolumn{5}{l}{\textbf{A: Use different time windows}} \\ ") 

restore

* 1.2 restricting the sample: 1993-2004
preserve

keep if WAVE>=1993 & WAVE<=2004

foreach y in $dependent{
eststo: xtreg `y' dd post $score i.WAVE, fe vce(cluster commid)
}

esttab est5 est6 est7 est8 using robust1.tex, b(a2) se(a2) keep(dd) starlevels(* 0.10 ** 0.05 *** 0.01)  ///
       fragment append nomtitles nonumbers noobs nolines nolabel

restore

* 1.3 restricting the sample: 1991-2006
preserve

keep if WAVE>=1991 & WAVE<=2006

foreach y in $dependent{
eststo: xtreg `y' dd post $score i.WAVE, fe vce(cluster commid)
}

esttab est9 est10 est11 est12 using robust1.tex, b(a2) se(a2) keep(dd) starlevels(* 0.10 ** 0.05 *** 0.01)  ///
       fragment append nomtitles nonumbers noobs nolines nolabel


* 2. create a fake treat group

* 2.1 data generation

use so2_match_wide.dta, clear

set seed 12345

gen double random = runiform()
sort random

egen count=count(commid)
levelsof count

gen byte falsegroup = _n <= `r(levels)'/2   /* assign half of the communities to treatment group */
label define falsegroup2 0 "Control" 1 "Treatment"
label values falsegroup falsegroup2
drop random

keep commid falsegroup
save falsegroup.dta, replace

* 2.2 creat a matched sample: PSM

use incdistribution.dta, clear
merge m:1 commid using falsegroup.dta, keep(match) nogen 
merge 1:1 commid WAVE using so2_match_long.dta, keep(match) nogen 

global score comm denc div econ health house market soc trans edc mart sani
keep commid WAVE $score gini so2* falsegroup

keep if WAVE<1998
collapse (mean) $score gini so2*, by(commid falsegroup)

psmatch2 falsegroup $score, outcome(gini so2_power) kernel common bwidth(0.01)
psgraph

gen common=_support
drop if common == 0 
drop if common ==.

keep commid falsegroup _pscore _treated _support _weight 
save kernalmatch2.dta, replace


* 2.3 PSM-DID using created sample

use incdistribution.dta, clear
merge m:1 commid using kernalmatch2.dta, keep(match) nogen
merge 1:1 commid WAVE using so2_match_long.dta, keep(match) nogen 

g post=WAVE>1998
g dd2=falsegroup*post

local varname gini theil pctratio_p90p10 pctratio_p75p25 
foreach var of local varname{
g log`var'=log(`var')	
}

xtset commid WAVE

eststo clear

foreach y in $dependent{
eststo: xtreg `y' dd2 post $score i.WAVE, fe vce(cluster commid)
}

esttab using robust2.tex, b(a2) se(a2) ar2 keep(dd2) starlevels(* 0.10 ** 0.05 *** 0.01) label nonumber replace ///
             posthead("\hline \multicolumn{5}{l}{\textbf{B: Creat a fake treatment group}} \\ ") 
			 

* 3. use 1989-1997 sample -- pre-regulation period

use incdistribution.dta, clear
merge 1:1 commid WAVE using so2_match_long.dta, keep(match) nogen
keep if WAVE<1998
save sample_pre1997.dta, replace

* 3.1 1993 as the first period after the introduction of the policy

* 3.1.1 PSM
keep commid WAVE $score so2*  tcz gini
keep if WAVE<1993  /*2 WAVES: 1989, 1991*/

global score comm denc div econ health house market soc trans edc mart sani

collapse (mean) $score gini so2*, by(commid tcz)
psmatch2 tcz $score, outcome(gini so2_power) kernel common bwidth(0.01)
psgraph

gen common=_support
drop if common == 0 
drop if common==.

keep commid tcz _pscore _treated _support _weight 
save kernalmatch3.dta, replace

* 3.1.2 PSM-DID

use sample_pre1997.dta, clear
merge m:1 commid using kernalmatch3.dta, keep(match) nogen
merge 1:1 commid WAVE using so2_match_long.dta, keep(match) nogen 

local varname gini theil pctratio_p90p10 pctratio_p75p25 
foreach var of local varname{
g log`var'=log(`var')	
}

xtset commid WAVE

g post3=WAVE>=1993
g dd3=tcz*post3

eststo clear
global score comm denc div econ health house market soc trans edc mart sani
local dependent loggini logtheil logpctratio_p90p10 logpctratio_p75p25

foreach y of local dependent{
eststo: xtreg `y' dd3 post3 $score i.WAVE, fe vce(cluster commid)
}

esttab using robust3a.tex, b(a2) se(a2) ar2 keep(dd3) starlevels(* 0.10 ** 0.05 *** 0.01) label nonumber replace ///
       posthead("\hline \multicolumn{5}{l}{\textbf{C: Use pre-regulation waves (1989--1997)}} \\ ") 


*3.2 1991 as the first period after the introduction of the policy

*3.2.1 PSM
 
use sample_pre1997.dta, clear

keep commid WAVE $score so2*  tcz gini
keep if WAVE<1991 

global score comm denc div econ health house market soc trans edc mart sani

collapse (mean) $score so2* gini, by(commid tcz)
psmatch2 tcz $score, outcome(gini so2_power) kernel common bwidth(0.01)
psgraph

gen common=_support
drop if common == 0 
drop if common==.

keep commid tcz _pscore _treated _support _weight 
save kernalmatch4.dta, replace

*3.2.2 PSM-DID

use sample_pre1997.dta, clear
merge m:1 commid using kernalmatch4.dta, keep(match) nogen
merge 1:1 commid WAVE using so2_match_long.dta, keep(match) nogen 

local varname gini theil pctratio_p90p10 pctratio_p75p25 
foreach var of local varname{
g log`var'=log(`var')	
}

xtset commid WAVE

g post4=WAVE>=1991
g dd4=tcz*post4

eststo clear
local dependent loggini logtheil logpctratio_p90p10 logpctratio_p75p25
foreach y of local dependent{
eststo: xtreg `y' dd4 post4 $score i.WAVE, fe vce(cluster commid)
}

esttab using robust3b.tex, b(a2) se(a2) ar2 keep(dd4) starlevels(* 0.10 ** 0.05 *** 0.01) label nonumber replace


* 4. include community-specific time trend

use analysis_main.dta, clear

g WAVE2=WAVE
tostring WAVE2, replace
encode WAVE2, g(t)

eststo clear
xtset commid WAVE
local dependent loggini logtheil logpctratio_p90p10 logpctratio_p75p25

foreach y of local dependent{
eststo: xtreg `y' dd post $score i.WAVE i.commid#c.t, fe vce(cluster commid)
}

esttab using robust4.tex, b(a2) se(a2) ar2 keep(dd) starlevels(* 0.10 ** 0.05 *** 0.01) nolabel nolines replace fragment ///
		posthead("\hline \multicolumn{5}{l}{\textbf{D: Include community $\times$ time trend}} \\ ") 

* 5. Std. errors clustered at the city level

eststo clear

tostring commid, replace
g citycode=substr(commid,1,4)
destring citycode commid, replace

local dependent loggini logtheil logpctratio_p90p10 logpctratio_p75p25
foreach y of local dependent{
eststo: xtreg `y' dd post $score i.WAVE, fe vce(cluster citycode)
}

esttab using robust5.tex, b(a2) se(a2) ar2 keep(dd) starlevels(* 0.10 ** 0.05 *** 0.01) replace ///
		posthead("\hline \multicolumn{5}{l}{\textbf{E: Cluster standard erros at the city level}} \\ ") 
		

		
		
		
